package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;


    @Override
    public ApartmentDetailVo getApartmentDetailVoById(Long id) {

        //实例化一个公寓详情的对象
        ApartmentDetailVo apartmentItemVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null) {
            return null;
        }
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        //获取公寓图片
        List<LabelInfo> labelInfoList = labelInfoService.getApartmentLabeInfo(id);
        //获取公寓图片信息
        List<GraphInfo> graphInfoList = graphInfoService.getApartmentGraphInfo(id);
        List<GraphVo> graphVos = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVos.add(graphVo);
        });


        //获取公寓配套信息
        List<FacilityInfo> facilityInfoList = apartmentFacilityService.getFacilityInfo(id);
        // 该公寓中所有房间的最少租金
        BigDecimal minRent = roomInfoMapper.getMinRentByApartmentId(id);
        apartmentItemVo.setMinRent(minRent);
        //公寓是否被删除
        apartmentItemVo.setIsDelete(apartmentInfo.getIsDeleted() ==1);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphVos);
        apartmentItemVo.setFacilityInfoList(facilityInfoList);
        apartmentItemVo.setMinRent(minRent);


        return apartmentItemVo;
    }

    @Override
    public ApartmentItemVo getApartmentDetailById(Long apartmentId) {

        //实例化一个公寓详情的对象
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        if (apartmentInfo == null) {
            return null;
        }
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        //获取公寓图片
        List<LabelInfo> labelInfoList = labelInfoService.getApartmentLabeInfo(apartmentId);
        //获取公寓图片信息
        List<GraphInfo> graphInfoList = graphInfoService.getApartmentGraphInfo(apartmentId);
//        List<GraphVo> graphVos = new ArrayList<>();
//        graphInfoList.forEach(graphInfo -> {
//            GraphVo graphVo = new GraphVo();
//            BeanUtils.copyProperties(graphInfo, graphVo);
//            graphVos.add(graphVo);
//        });


//        //获取公寓配套信息
//        List<FacilityInfo> facilityInfoList = apartmentFacilityService.getFacilityInfo(apartmentId);
        // 该公寓中所有房间的最少租金
        BigDecimal minRent = roomInfoMapper.getMinRentByApartmentId(apartmentId);
        apartmentItemVo.setMinRent(minRent);
        //公寓是否被删除
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }


}




